package com.fzh.wangxiao.web.dispatch.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzh.wangxiao.admin.service.UserService;
import com.fzh.wangxiao.utils.Result;
import com.fzh.wangxiao.web.dispatch.entity.TBizDispatch;
import com.fzh.wangxiao.web.dispatch.entity.TBizDispatchRecord;
import com.fzh.wangxiao.web.dispatch.mapper.TBizDispatchRecordMapper;
import com.fzh.wangxiao.web.dispatch.service.TBizDispatchRecordService;
import com.fzh.wangxiao.web.req.DispatchRecordRequest;
import com.fzh.wangxiao.web.req.FollowListRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fang
 * @since 2021-01-05
 */
@Service
public class TBizDispatchRecordServiceImpl extends ServiceImpl<TBizDispatchRecordMapper, TBizDispatchRecord> implements TBizDispatchRecordService {


    @Autowired
    private UserService userService;

    @Override
    public Result followUpNotes(DispatchRecordRequest recordRequest) {
        Result result = new Result();
        TBizDispatchRecord record = new TBizDispatchRecord();
        record.setDispatchId(record.getDispatchId());
        record.setRemarks(record.getRemarks());
        baseMapper.insert(record);
        return result;
    }


    @Override
    public Result followLog(FollowListRequest request) {
        Result result = new Result();
        QueryWrapper<TBizDispatchRecord> wrapper = new QueryWrapper();
        wrapper.eq("dispatch_id",request.getDispatchId());
        wrapper.orderByDesc("create_time");
        Page<TBizDispatchRecord> pages = new Page<>(request.getPageNo(),request.getPageSize());
        IPage<TBizDispatchRecord> mapIPage = baseMapper.selectPage(pages, wrapper);
        result.setData(mapIPage.getRecords());
        result.setTotals(mapIPage.getTotal());
        return result;
    }

    @Override
    public Result followByMid(FollowListRequest request) {
        Result result = new Result();
        QueryWrapper<TBizDispatchRecord> wrapper = new QueryWrapper();
        wrapper.eq("member_id",request.getMemberId());
        wrapper.orderByDesc("create_time");
        Page<TBizDispatchRecord> pages = new Page<>(request.getPageNo(),request.getPageSize());
        IPage<TBizDispatchRecord> mapIPage = baseMapper.selectPage(pages, wrapper);

        result.setTotals(mapIPage.getTotal());
        List<TBizDispatchRecord> list = mapIPage.getRecords();
        for(int i=0;i<list.size();i++){
            list.get(i).setUserName(userService.getUserName(list.get(i).getUserId()));
        }
        result.setData(list);
        return result;
    }


    @Override
    public void addfollowUpNotes(TBizDispatchRecord record) {


        baseMapper.insert(record);
    }
}
